package com.jiyinit.web.core.common.jsptag;

import com.jiyinit.core.framework.base.util.Check;
import com.jiyinit.core.framework.base.util.LogUtil;
import com.jiyinit.web.core.business.entity.TabCoreDictItemEntity;
import com.jiyinit.web.core.business.service.DictItemServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoader;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.util.List;

/**
 * <p></p>
 * <p>
 * <PRE>
 * <BR>	修改记录
 * <BR>-----------------------------------------------
 * <BR>	修改日期			修改人			修改内容
 * </PRE>
 *
 * @author lishaochuan
 * @version 1.0
 * @date 2017/4/24 15:41
 * @since 1.0
 */
public class DictTag extends TagSupport {

    /**
     * 日志对象
     */
    private static Logger LOGGER = LoggerFactory.getLogger(DictTag.class);

    /**
     * 查询字典项service
     */
    private DictItemServiceImpl dictItemService;

    /**
     * 字典编码
     */
    private String dictCode;

    /**
     * 默认选中值
     */
    private String value;

    /**
     * 是否展示请选择
     */
    private Boolean showChoose;

    @Override
    public int doAfterBody() throws JspException {
        return super.doAfterBody();
    }

    @Override
    public int doEndTag() throws JspException {
        return super.doEndTag();
    }

    @Override
    public int doStartTag() throws JspException {
        try {
            init();
            JspWriter out = pageContext.getOut();
            String option = this.option();
            out.println(option);
        }catch (Exception e){
            LogUtil.error(LOGGER, "select转换字典值错误:{}", e);
        }
        return TagSupport.SKIP_BODY;
    }


    /**
     * 初始化service
     * @author lishaochuan
     * @create 2017/4/24 16:54
     * @param
     * @return
     */
    private void init(){
        dictItemService = (DictItemServiceImpl) ContextLoader.getCurrentWebApplicationContext().getBean("core.dictItemServiceImpl");
    }

    /**
     * 拼装select的option
     * @author lishaochuan
     * @create 2017/4/24 16:56
     * @param
     * @return
     */
    private String option(){
        StringBuilder str = new StringBuilder();
        List<TabCoreDictItemEntity> dictItemEntities = dictItemService.selectByDictCode(dictCode);
        if(!Check.isNullObj(showChoose) && showChoose){
            str.append("<option value=\"\">请选择</option>\n");
        }
        for (TabCoreDictItemEntity dictItemEntity : dictItemEntities) {
            str.append("<option value=\"" + dictItemEntity.getItemValue() + "\" "+getIsSelected(value, dictItemEntity.getItemValue())+">" + dictItemEntity.getItemName() + "</option>\n");
        }
        return str.toString();
    }

    /**
     * 判断是否默认选中
     * @author lishaochuan
     * @create 2017/4/24 16:56
     * @param
     * @return
     */
    private static String getIsSelected(String value, String selectValue) {
        if(!Check.isNullStr(value) && value.equals(selectValue)) {
            return "selected=\"selected\"";
        }
        return "";
    }



    public String getDictCode() {
        return dictCode;
    }

    public void setDictCode(String dictCode) {
        this.dictCode = dictCode;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public Boolean getShowChoose() {
        return showChoose;
    }

    public void setShowChoose(Boolean showChoose) {
        this.showChoose = showChoose;
    }
}
